package com.aiton.bamin.bamindaijiadrier.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.bamin.bamindaijiadrier.R;
import com.aiton.bamin.bamindaijiadrier.adapter.MyMessageAdapter;
import com.aiton.bamin.bamindaijiadrier.constant.Constant;
import com.aiton.bamin.bamindaijiadrier.model.MessageList;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class MessageActivity extends ZjbBaseActivity implements View.OnClickListener {
    private ListView mList_message;
    private List<MessageList.DatasBean> mDatas = new ArrayList();
    private MyMessageAdapter mMyMessageAdapter;
    private TextView mTv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        init();
    }

    @Override
    protected void initData() {
        getMessageList();
    }

    private void getMessageList() {
        String url = Constant.Url.APP_DRIVER_GET_MESSAGE;
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String s = new String(responseBody);
                try {
                    MessageList messageList = GsonUtils.parseJSON(s, MessageList.class);
                    if(messageList.getStatus() == 0){
                        List<MessageList.DatasBean> datas = messageList.getDatas();
                        mDatas.addAll(datas);
                        mMyMessageAdapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    Toast.makeText(MessageActivity.this, "数据解析失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(MessageActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initViews() {
        mMyMessageAdapter = new MyMessageAdapter(this,mDatas);
        mList_message.setAdapter(mMyMessageAdapter);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);


    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void findID() {
        mList_message = (ListView) findViewById(R.id.list_message);
        mTv_title = (TextView) findViewById(R.id.tv_title);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;
        }
    }



}
